package com.example.jobback.service.seeker.impl;

import com.example.jobback.po.*;
import com.example.jobback.req.seeker.company.GetPageCompaniesReq;
import com.example.jobback.resp.seeker.company.*;
import com.example.jobback.result.Result;
import com.example.jobback.service.common.UserCommonService;
import com.example.jobback.service.recruiter.RecruiterCrossService;
import com.example.jobback.service.seeker.SeekerCompanyService;
import com.example.jobback.util.CACHEUtil;
import com.example.jobback.util.TLUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import static com.example.jobback.util.RedisConstants.HOT_COMPANY_CITIES_KEY;

/**
 * @ClassName: SeekerCompanyServiceImpl
 * @Description:求职者-公司接口实现类
 * @Author: lhb
 * @Date: 2025/7/23
 */
@Service
public class SeekerCompanyServiceImpl implements SeekerCompanyService {

    private final RecruiterCrossService recruiterCrossService;
    
    private final UserCommonService userCommonService;

    private final CACHEUtil cacheUtil;

    public SeekerCompanyServiceImpl(RecruiterCrossService recruiterCrossService, UserCommonService userCommonService, CACHEUtil cacheUtil) {
        this.recruiterCrossService = recruiterCrossService;
        this.userCommonService = userCommonService;
        this.cacheUtil = cacheUtil;
    }

    @Override
    public Result<GetPageCompaniesResp> getCompanies(GetPageCompaniesReq req) {
        PageInfo<Company> companyPageInfo = recruiterCrossService.getCompanies(req);
        GetPageCompaniesResp resp = new GetPageCompaniesResp();
        resp.setSize(companyPageInfo.getSize());
        resp.setTotal(companyPageInfo.getTotal());
        resp.setPages(companyPageInfo.getPages());
        resp.setPageNum(companyPageInfo.getPageNum());
        resp.setPageSize(companyPageInfo.getPageSize());
        List<Company> companyList = companyPageInfo.getList();
        resp.setRespList(new ArrayList<>());
        for (Company company : companyList) {
            GetPageCompanyResp getPageCompanyResp = new GetPageCompanyResp();
            BeanUtils.copyProperties(company, getPageCompanyResp);
            resp.getRespList().add(getPageCompanyResp);
        }
        return Result.success(resp);
    }

    /**
     * @Description: 获取公司详情
     * @param companyId
     * @return Result<CompetailResp>
     * @Author lhb
     * @CreateTime 2025/7/23 14:44
     */
    @Override
    public Result<CompanyDetailResp> getCompanyDetail(Long companyId) {
        Company company = recruiterCrossService.getCompanyMessage(companyId);
        if(Objects.isNull(company)) {
            throw new NullPointerException("该公司不存在");
        }
        CompanyDetailResp resp = new CompanyDetailResp();
        resp.setCompanyPositionRespList(new ArrayList<>());
        resp.setCompanyRecruiterRespList(new ArrayList<>());
        List<Recruiter> recruiterList = recruiterCrossService.getRecruitersByCompanyId(companyId);
        List<Long> userIds = recruiterList.stream()
                .map(Recruiter::getUserId)
                .filter(Objects::nonNull)
                .toList();
        List<User> userList = userCommonService.getUsersByUserIds(userIds);
        Map<Long, User> userMap = userList.stream()
                .collect(Collectors.toMap(User::getUserId, user -> user));
        List<Position> positionList = recruiterCrossService.getPositionByCompanyId(companyId);
        BeanUtils.copyProperties(company, resp);
        for (Recruiter recruiter : recruiterList) {
            User user = userMap.get(recruiter.getUserId());
            BeanUtils.copyProperties(user, recruiter);
            CompanyRecruiterResp companyRecruiterResp = new CompanyRecruiterResp();
            BeanUtils.copyProperties(recruiter, companyRecruiterResp);
            resp.getCompanyRecruiterRespList().add(companyRecruiterResp);
        }
        Map<Long, Recruiter> recruiterMap = recruiterList.stream()
                .collect(Collectors.toMap(Recruiter::getUserId, recruiter -> recruiter));
        for (Position position : positionList) {
            CompanyPositionResp companyPositionResp = new CompanyPositionResp();
            companyPositionResp.setCompanyId(companyId);
            companyPositionResp.setCompanyName(company.getName());
            companyPositionResp.setCompanyAvatar(company.getAvatar());
            BeanUtils.copyProperties(position, companyPositionResp);
            Recruiter recruiter = recruiterMap.get(position.getUserId());
            companyPositionResp.setUserName(recruiter.getName());
            companyPositionResp.setRole(recruiter.getRole());
            companyPositionResp.setAvatar(recruiter.getAvatar());
            resp.getCompanyPositionRespList().add(companyPositionResp);
        }
        return Result.success(resp);
    }

    /**
     * @Description:  获取热门企业城市
     * @return Result<Object>
     * @Author lhb
     * @CreateTime 2025/7/23 16:09
     */
    @Override
    public Result<Object> getHotCityList() {
        Set<String> hotCityList = cacheUtil.getAllMembersFromSet(HOT_COMPANY_CITIES_KEY);
        return Result.success(hotCityList);
    }

    /**
     * @Description:  智能推荐企业
     * @return Result<List<SmartReendComp>
     * @Author lhb
     * @CreateTime 2025/7/25 9:39
     */
    @Override
    public Result<List<SmartRecommendCompanyResp>> smartRecommendCompanyList() {
        List<SmartRecommendCompanyResp> respList = new ArrayList<>();
        Seeker seeker = TLUtil.get(TLUtil.S);
        List<Company> companyList = recruiterCrossService.smartRecommendCompanyList(seeker);
        for (Company company : companyList) {
            SmartRecommendCompanyResp resp = new SmartRecommendCompanyResp();
            BeanUtils.copyProperties(company, resp);
            respList.add(resp);
        }
        return Result.success(respList);
    }

}
